//
//  MHUserInfoViewModel.m
// MiaoCircle
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHUserInfoViewModel.h"
#import "MHCommonAvatarItemViewModel.h"
#import "MHCommonLabelItemViewModel.h"
#import "MHCommonQRCodeItemViewModel.h"
#import "MHPhotoManager.h"
#import "MHCustomTextFeildViewModel.h"
//#import "MHMoreInfoViewModel.h"
//#import "MHModifyNicknameViewModel.h"
@interface MHUserInfoViewModel ()
/// The current `user`.
@property (nonatomic, readwrite , strong) MHUser *user;
@end

@implementation MHUserInfoViewModel

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
        /// 获取user
        self.user = params[MHViewModelUtilKey];
    }
    return self;
}

- (void)initialize{
    [super initialize];
    @weakify(self);
    self.title = @"个人信息";
    
    self.rightTitle = @"保存";
    
    /// 第一组
    MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
    /// 头像
    MHCommonAvatarItemViewModel *avatar = [MHCommonAvatarItemViewModel itemViewModelWithTitle:@"头像"];
    avatar.avatar = self.user.allHeadPath;
    avatar.hiddenArrow = YES;
    avatar.rowHeight = 83.0f;
   
    
    self.uploadProfileInfoCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [[self.services.client modifyUserProfileWithHeadPic:self.uploadPic nickName:self.nickName] doNext:^(id x) {
            if (self.headPic.length > 0) {
                 [MHHTTPService sharedInstance].currentUser.allHeadPath = self.headPic;
            }
            [MHHTTPService sharedInstance].currentUser.screenName = self.nickName;
            MHUser *user = [MHHTTPService sharedInstance].currentUser;
            [self.services.client loginUser:user];
            [self.services popViewModelAnimated:YES];
        }];
    }];
    
    
    self.uploadImageCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        self.services.client.swithUrlWithUpload = YES;
        return    [self.services.client uploadUserFaceWith:nil fileDatas:input miniType:@""];
    }];
    
    /// 名字
    MHCustomTextFeildViewModel *nickname = [MHCustomTextFeildViewModel itemViewModelWithTitle:@"名字"];
//    nickname.subtitle = self.user.screenName;
    nickname.nameText = self.user.screenName;
    nickname.hiddenArrow = YES;
    @weakify(nickname);
    nickname.operation = ^{
//        @strongify(self);
//        @strongify(nickname);
//        NSString *value = MHStringIsNotEmpty(self.user.screenName)?self.user.screenName:@"";
//        MHModifyNicknameViewModel *viewModel = [[MHModifyNicknameViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:value}];
//        [self.services presentViewModel:viewModel animated:YES completion:NULL];
//
//        /// 设置block
//        @weakify(self);
//        @weakify(nickname);
//        viewModel.callback = ^(NSString *screenName) {
//            @strongify(self);
//            @strongify(nickname);
//            self.user.screenName = screenName;
//            [[self.services client] saveUser:self.user];
//            nickname.subtitle = screenName;
//
//            // “手动触发self.dataSource的KVO”，必写。
//            [self willChangeValueForKey:@"dataSource"];
//            // “手动触发self.now的KVO”，必写。
//            [self didChangeValueForKey:@"dataSource"];
//        };
    };
    
    /// 微信号
    MHCommonItemViewModel *wechatId = [MHCommonItemViewModel itemViewModelWithTitle:@"微信号"];
    wechatId.selectionStyle = UITableViewCellSelectionStyleNone;
    wechatId.subtitle = self.user.wechatId;
    
    /// 二维码
    MHCommonQRCodeItemViewModel *qrCode = [MHCommonQRCodeItemViewModel itemViewModelWithTitle:@"我的二维码"];
    
    /// 更多
//    MHCommonArrowItemViewModel *moreInfo = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"更多"];
//    /// 将用户数据 传递过去
//    moreInfo.destViewModelClass = [MHMoreInfoViewModel class];
    
    
    
    group0.itemViewModels = @[avatar , nickname];
    
    /// 第二组
    MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
    /// 我的地址
    MHCommonArrowItemViewModel *address = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"我的地址"];
    group1.itemViewModels = @[address];
    
    self.dataSource = @[group0];
}

@end
